import network
import time


def wifi_scan():
    """简单的WiFi扫描函数"""
    print("扫描WiFi网络...")
    
    wlan = network.WLAN(network.STA_IF)
    wlan.active(True)
    
    networks = wlan.scan()
    
    if not networks:
        print("未发现WiFi网络")
        return []
    
    print(f"发现 {len(networks)} 个网络:")
    
    # 按信号强度排序
    networks.sort(key=lambda x: x[3], reverse=True)
    
    for i, net in enumerate(networks, 1):
        ssid = net[0].decode('utf-8') if net[0] else "隐藏"
        rssi = net[3]
        security = net[4]
        
        # 信号强度百分比
        signal = min(100, max(0, (rssi + 100) * 2))
        
        # 加密类型
        if security == 0:
            sec_type = "开放"
        elif security == 3:
            sec_type = "WPA2"
        else:
            sec_type = "加密"
        
        print(f"{i:2d}. {ssid:<20} {signal:3d}% {sec_type}")
    
    return networks


if __name__ == "__main__":
    wifi_scan()
